package com.example.day_20.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.day_20.R;
import com.example.day_20.bean.InFoBean;

import java.util.ArrayList;
import java.util.List;

public class RlvHomeAdapter extends RecyclerView.Adapter {

    private Context context;
    public List<InFoBean.DataBean.DatasBean> datas = new ArrayList<>();
    public static final int TYPE_ONE = 0;
    public static final int TYPE_TWO = 1;

    public RlvHomeAdapter(Context context) {
        this.context = context;
    }

    public void setDatas(List<InFoBean.DataBean.DatasBean> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TYPE_ONE) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_home_one, viewGroup, false);
            return new ViewHolderOne(inflate);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_home_two, viewGroup, false);
            return new ViewHolderTwo(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        InFoBean.DataBean.DatasBean datasBean = datas.get(i);
        int type = getItemViewType(i);
        switch (type) {
            case TYPE_ONE:
                ViewHolderOne one = (ViewHolderOne) viewHolder;
                one.tv_one.setText(datasBean.getTitle());
                Glide.with(context).load(datasBean.getEnvelopePic()).into(one.iv_one);
                break;
            case TYPE_TWO:
                ViewHolderTwo two = (ViewHolderTwo) viewHolder;
                two.tv_two.setText(datasBean.getTitle());
                Glide.with(context).load(datasBean.getEnvelopePic()).into(two.iv_two);
                break;
        }

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null){
                    onItemLongClickListener.onLongClick(v,i);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolderOne extends RecyclerView.ViewHolder {
        ImageView iv_one;
        TextView tv_one;

        public ViewHolderOne(@NonNull View itemView) {
            super(itemView);
            iv_one = itemView.findViewById(R.id.iv_one);
            tv_one = itemView.findViewById(R.id.tv_one);
        }
    }

    public class ViewHolderTwo extends RecyclerView.ViewHolder {
        ImageView iv_two;
        TextView tv_two;

        public ViewHolderTwo(@NonNull View itemView) {
            super(itemView);
            iv_two = itemView.findViewById(R.id.iv_two);
            tv_two = itemView.findViewById(R.id.tv_two);
        }
    }

    onItemLongClickListener onItemLongClickListener;

    public void setOnItemLongClickListener(RlvHomeAdapter.onItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface onItemLongClickListener{
        void onLongClick(View v,int position);
    }
}
